package com.xcity.game.combat.unit.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.game.combat.AbstractCombatUnit;
import com.xcity.game.combat.Combat;
import com.xcity.game.combat.CombatTeam;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.skill.CombatSkillFactory;
import com.xcity.game.employee.Clazz;
import com.xcity.game.npc.NpcCombatable;
import com.xcity.game.npc.NpcSkill;
import com.xcity.game.npc.NpcTemplate;
import com.xcity.game.skill.template.SkillTemplate;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.util.CommonUtils;

/**
 * npc战斗对象
 * @author yang.li
 *
 */
public class NpcCombatUnit extends AbstractCombatUnit<NpcCombatable> {

	protected int id;
	protected NpcTemplate template;
	public static final AtomicInteger IDS = new AtomicInteger();
	
	public NpcCombatUnit(CombatTeam team, NpcTemplate template) {
		this(team, template, false);
	}
	
	public NpcCombatUnit(CombatTeam team, NpcTemplate template, boolean boss) {
		super(team, (NpcCombatable) template.toCombatabale());
		this.id = IDS.incrementAndGet();
		this.template = template;
		setAttributeValue(CombatAttributeType.ATTACK_POWER, 0);
		setAttributeValue(CombatAttributeType.MAGIC_POWER, 0);
		float val = CommonUtils.randomInt(Combat.RND, template.getMinDefense(), template.getMaxDefense());
		setAttributeValue(CombatAttributeType.DEFENSE, val);
		val = CommonUtils.randomInt(Combat.RND, template.getMinMagicDefense(), template.getMaxMagicDefense());
		setAttributeValue(CombatAttributeType.MAGIC_DEFENSE, val);
		setAttributeValue(CombatAttributeType.CRIT, template.getCrit());
		setAttributeValue(CombatAttributeType.AVOID_CRIT, template.getAvoidCrit());
		setAttributeValue(CombatAttributeType.ATTACK_SPEED, template.getAttackSpeed());
		setAttributeValue(CombatAttributeType.CRIT_DAMAGE, 0);
		val = CommonUtils.randomInt(Combat.RND, template.getMinHp(), template.getMaxHp());
		setAttributeValue(CombatAttributeType.MAX_HP, val);
		setAttributeValue(CombatAttributeType.HP, val);
//		setAttributeValue(CombatAttributeType.MP, 0);
//		setAttributeValue(CombatAttributeType.MAX_MP, 0);
		setAttributeValue(CombatAttributeType.DODGE, template.getDodge());
		setAttributeValue(CombatAttributeType.RESIST, template.getResist());
		setAttributeValue(CombatAttributeType.HIT_RATE, template.getHitRate());
		setAttributeValue(CombatAttributeType.MAGIC_HIT_RATE, template.getMagicHitRate());
		
		List<NpcSkill> npcSkills = combatable.getSkills();
		if (npcSkills.isEmpty()) {
			return;
		}
		this.skills = new ArrayList<CombatSkill>(1);
		int i = CommonUtils.randomInt(npcSkills.size());
		CombatSkill cs = CombatSkillFactory.create(npcSkills.get(i));
		if (cs != null) {
			this.skills.add(cs);
			setAttributeValue(CombatAttributeType.MAX_MP, cs.getMp());
		}
	}

	@Override
	public long getId() {
		return id;
	}

	@Override
	@JSONField(serialize = false)
	public float getAttackPower() {
		int val = CommonUtils.randomInt(Combat.RND, template.getMinAttackPower(), template.getMaxAttackPower());
		return val + super.getAttackPower();
	}

	@Override
	@JSONField(serialize = false)
	public float getMagicPower() {
		int val = CommonUtils.randomInt(Combat.RND, template.getMinMagicPower(), template.getMaxMagicPower());
		return val + super.getMagicPower();
	}

	@Override
	public Clazz getClazz() {
		return Clazz.WARRIOR;
	}

}
